package cn.fangline.wo.task.akka.actors;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import cn.fangline.wo.task.akka.messages.CheckAlive;
import cn.fangline.wo.task.akka.messages.EndPoint;
import cn.fangline.wo.task.akka.messages.HouseImagePage;

/**
 * Created by 抽离 on 2017/6/28 0028.
 */
public class HouseImageMasterActor extends AbstractActor {

    private boolean isAlive = true;

    private ActorRef houseImageRef;

    public HouseImageMasterActor(ActorRef houseImageRef) {
        this.houseImageRef = houseImageRef;
    }

    @Override
    public Receive createReceive() {
        return
                receiveBuilder()
                        .match(HouseImagePage.class, this::onReceiveImagePage)
                        .match(CheckAlive.class, this::onReceiveCheckAlive)
                        .match(EndPoint.class, this::onReceiveEndPoint)
                        .build();
    }

    private  void onReceiveEndPoint(EndPoint m) {
        isAlive = true;
    }

    private void onReceiveCheckAlive(Object m) {
        if(isAlive) {
            getSender().tell(true, getSelf());
            isAlive = false;
        }else{
            getSender().tell(false, getSelf());
        }
    }

    private void onReceiveImagePage(HouseImagePage houseImagePage) {
        houseImageRef.tell(houseImagePage, getSelf());
    }


}
